/**
 * 修复天赋石基础数据脚本
 * 重新创建正确的天赋石数据
 */

const mysql = require('mysql2/promise');
const config = require('./database-config.js');

const dbConfig = config.source;

// 天赋石等级元气数据 (根据文档)
const talentStoneLevelData = [
    { level: 1, energy: 100, silverCost: 0 },
    { level: 2, energy: 100, silverCost: 1000 },
    { level: 4, energy: 331, silverCost: 6000 },
    { level: 6, energy: 610, silverCost: 15000 },
    { level: 8, energy: 948, silverCost: 28000 },
    { level: 10, energy: 1357, silverCost: 45000 },
    { level: 12, energy: 1852, silverCost: 66000 },
    { level: 14, energy: 2451, silverCost: 91000 },
    { level: 16, energy: 3176, silverCost: 120000 },
    { level: 18, energy: 4053, silverCost: 153000 },
    { level: 20, energy: 5114, silverCost: 190000 },
    { level: 22, energy: 6344, silverCost: 231000 },
    { level: 24, energy: 7598, silverCost: 276000 },
    { level: 26, energy: 8877, silverCost: 325000 },
    { level: 28, energy: 10182, silverCost: 378000 },
    { level: 30, energy: 11513, silverCost: 435000 },
    { level: 32, energy: 12871, silverCost: 496000 },
    { level: 34, energy: 14256, silverCost: 561000 },
    { level: 36, energy: 15669, silverCost: 630000 },
    { level: 38, energy: 17110, silverCost: 703000 },
    { level: 40, energy: 18581, silverCost: 780000 },
    { level: 42, energy: 20081, silverCost: 861000 },
    { level: 44, energy: 21611, silverCost: 946000 },
    { level: 46, energy: 23172, silverCost: 1035000 },
    { level: 48, energy: 24764, silverCost: 1128000 },
    { level: 50, energy: 26388, silverCost: 1225000 },
    { level: 52, energy: 28045, silverCost: 1326000 },
    { level: 54, energy: 29735, silverCost: 1431000 },
    { level: 56, energy: 31459, silverCost: 1540000 },
    { level: 58, energy: 33218, silverCost: 1653000 },
    { level: 60, energy: 35013, silverCost: 1770000 },
    { level: 62, energy: 36844, silverCost: 1891000 },
    { level: 64, energy: 38711, silverCost: 2016000 },
    { level: 66, energy: 40616, silverCost: 2145000 },
    { level: 68, energy: 42559, silverCost: 2278000 },
    { level: 70, energy: 44541, silverCost: 2415000 },
    { level: 72, energy: 46563, silverCost: 2556000 },
    { level: 74, energy: 48625, silverCost: 2701000 },
    { level: 76, energy: 50729, silverCost: 2850000 },
    { level: 78, energy: 52875, silverCost: 3003000 },
    { level: 80, energy: 55064, silverCost: 3160000 },
    { level: 82, energy: 57297, silverCost: 3321000 },
    { level: 84, energy: 59575, silverCost: 3486000 },
    { level: 86, energy: 61899, silverCost: 3655000 },
    { level: 88, energy: 64269, silverCost: 3828000 },
    { level: 90, energy: 66687, silverCost: 4005000 },
    { level: 92, energy: 69154, silverCost: 4186000 },
    { level: 94, energy: 71671, silverCost: 4371000 },
    { level: 96, energy: 74238, silverCost: 4560000 },
    { level: 98, energy: 76857, silverCost: 4753000 },
    { level: 100, energy: 79528, silverCost: 4950000 },
    { level: 102, energy: 82375, silverCost: 5151000 },
    { level: 104, energy: 85819, silverCost: 5356000 },
    { level: 106, energy: 89987, silverCost: 5565000 },
    { level: 108, energy: 95031, silverCost: 5778000 },
    { level: 110, energy: 101134, silverCost: 5995000 },
    { level: 112, energy: 108518, silverCost: 6216000 },
    { level: 114, energy: 117453, silverCost: 6441000 },
    { level: 116, energy: 128264, silverCost: 6670000 },
    { level: 118, energy: 141346, silverCost: 6903000 },
    { level: 120, energy: 157175, silverCost: 7140000 },
    { level: 122, energy: 175508, silverCost: 7381000 },
    { level: 124, energy: 194209, silverCost: 7626000 },
    { level: 126, energy: 213286, silverCost: 7875000 },
    { level: 128, energy: 232747, silverCost: 8128000 },
    { level: 130, energy: 252598, silverCost: 8385000 },
    { level: 132, energy: 272849, silverCost: 8646000 },
    { level: 134, energy: 293506, silverCost: 8911000 },
    { level: 136, energy: 314579, silverCost: 9180000 },
    { level: 138, energy: 336076, silverCost: 9453000 },
    { level: 140, energy: 358005, silverCost: 9730000 },
    { level: 142, energy: 380374, silverCost: 10011000 },
    { level: 144, energy: 403193, silverCost: 10296000 },
    { level: 146, energy: 426471, silverCost: 10585000 },
    { level: 148, energy: 450217, silverCost: 10878000 },
    { level: 150, energy: 474440, silverCost: 11175000 },
    { level: 152, energy: 499149, silverCost: 11476000 },
    { level: 154, energy: 524354, silverCost: 11781000 },
    { level: 156, energy: 550066, silverCost: 12090000 },
    { level: 158, energy: 576296, silverCost: 12403000 },
    { level: 160, energy: 603053, silverCost: 12720000 }
];

// 天赋石基础配置数据
const talentStoneBaseData = [
    // 武士职业
    { typeId: 1, name: "舍命石", description: "提高致命威力", roleType: 1, baseEffect: 5.0, effectType: 1 },
    { typeId: 2, name: "强攻石", description: "提升攻击力", roleType: 1, baseEffect: 10.0, effectType: 1 },
    { typeId: 3, name: "舍攻石", description: "转化攻击力", roleType: 1, baseEffect: 20.0, effectType: 1 },
    { typeId: 4, name: "忽视石", description: "提高忽视几率", roleType: 1, baseEffect: 10.0, effectType: 1 },
    { typeId: 5, name: "暴击石", description: "提高暴击威力", roleType: 1, baseEffect: 25.0, effectType: 1 },
    { typeId: 6, name: "合击石", description: "提高合击几率", roleType: 1, baseEffect: 10.0, effectType: 1 },

    // 异人职业
    { typeId: 10, name: "法爆石", description: "提高法术暴击几率", roleType: 3, baseEffect: 4.5, effectType: 1 },
    { typeId: 11, name: "毒术石", description: "提升毒术威力", roleType: 3, baseEffect: 5.0, effectType: 1 },
    { typeId: 12, name: "风沙石", description: "提升风沙威力", roleType: 3, baseEffect: 5.0, effectType: 1 },
    { typeId: 13, name: "妖火石", description: "提升妖火威力", roleType: 3, baseEffect: 5.0, effectType: 1 },
    { typeId: 14, name: "落雷石", description: "提升落雷威力", roleType: 3, baseEffect: 5.0, effectType: 1 },

    // 文人职业
    { typeId: 15, name: "扰乱石", description: "提升扰乱威力", roleType: 2, baseEffect: 5.0, effectType: 1 },
    { typeId: 16, name: "封锁石", description: "提升封锁威力", roleType: 2, baseEffect: 5.0, effectType: 1 },
    { typeId: 17, name: "围困石", description: "提升围困威力", roleType: 2, baseEffect: 5.0, effectType: 1 },
    { typeId: 18, name: "暗渡石", description: "提升暗渡威力", roleType: 2, baseEffect: 5.0, effectType: 1 },
    { typeId: 19, name: "夺命石", description: "提升夺命程度", roleType: 2, baseEffect: 28.0, effectType: 1 },

    // 通用职业
    { typeId: 20, name: "强血石", description: "增加生命值", roleType: 0, baseEffect: 5.0, effectType: 1 },
    { typeId: 21, name: "反击石", description: "提高反击几率", roleType: 0, baseEffect: 10.0, effectType: 1 },
    { typeId: 22, name: "爆率石", description: "提高爆率", roleType: 0, baseEffect: 5.0, effectType: 1 },
    { typeId: 23, name: "致命石", description: "提高致命几率", roleType: 0, baseEffect: 10.0, effectType: 1 },
    { typeId: 24, name: "躲避石", description: "提高躲避几率", roleType: 0, baseEffect: 10.0, effectType: 1 }
];

async function fixTalentStoneData() {
    let connection;

    try {
        console.log('🔌 连接数据库...');
        connection = await mysql.createConnection(dbConfig);
        console.log('✅ 数据库连接成功');

        // 删除现有表
        console.log('🗑️ 删除现有表...');
        await connection.execute('DROP TABLE IF EXISTS talent_stone_level');
        await connection.execute('DROP TABLE IF EXISTS talent_stone_base');
        await connection.execute('DROP TABLE IF EXISTS talent_stone');

        // 创建天赋石等级表
        console.log('📊 创建天赋石等级表...');
        await connection.execute(`
      CREATE TABLE talent_stone_level (
        level INT PRIMARY KEY COMMENT '等级',
        energy INT NOT NULL COMMENT '该等级需要的元气值',
        silver_cost INT NOT NULL COMMENT '升级到该等级消耗的银两',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        updated_at TIMESTAMP NULL COMMENT '更新时间'
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='天赋石等级元气表'
    `);

        // 创建天赋石基础配置表
        console.log('📊 创建天赋石基础配置表...');
        await connection.execute(`
      CREATE TABLE talent_stone_base (
        type_id INT PRIMARY KEY COMMENT '天赋石类型ID',
        name VARCHAR(50) NOT NULL COMMENT '天赋石名称',
        description VARCHAR(200) NOT NULL COMMENT '天赋石描述',
        role_type INT NOT NULL COMMENT '适用职业',
        base_effect DECIMAL(10,2) NOT NULL COMMENT '基础效果值',
        effect_type INT NOT NULL COMMENT '效果类型',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        updated_at TIMESTAMP NULL COMMENT '更新时间'
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='天赋石基础配置表'
    `);

        // 创建天赋石表
        console.log('📊 创建天赋石表...');
        await connection.execute(`
      CREATE TABLE talent_stone (
        id INT AUTO_INCREMENT PRIMARY KEY COMMENT '天赋石ID',
        character_id INT NOT NULL COMMENT '角色ID',
        name VARCHAR(50) NOT NULL COMMENT '天赋石名称',
        level INT DEFAULT 1 COMMENT '天赋石等级',
        energy INT DEFAULT 100 COMMENT '元气值',
        type INT NOT NULL COMMENT '天赋石类型',
        state INT DEFAULT 0 COMMENT '天赋石状态',
        deputy_id INT NULL COMMENT '装备的副将ID',
        description VARCHAR(200) NULL COMMENT '描述',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
        updated_at TIMESTAMP NULL COMMENT '更新时间',
        INDEX idx_character_name (character_id, name)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='天赋石表'
    `);

        console.log('✅ 表结构创建完成');

        // 插入等级元气数据
        console.log('📝 插入天赋石等级元气数据...');
        for (const data of talentStoneLevelData) {
            await connection.execute(
                'INSERT INTO talent_stone_level (level, energy, silver_cost) VALUES (?, ?, ?)',
                [data.level, data.energy, data.silverCost]
            );
        }
        console.log(`✅ 插入了${talentStoneLevelData.length}条等级元气数据`);

        // 插入基础配置数据
        console.log('📝 插入天赋石基础配置数据...');
        for (const data of talentStoneBaseData) {
            await connection.execute(
                'INSERT INTO talent_stone_base (type_id, name, description, role_type, base_effect, effect_type) VALUES (?, ?, ?, ?, ?, ?)',
                [data.typeId, data.name, data.description, data.roleType, data.baseEffect, data.effectType]
            );
        }
        console.log(`✅ 插入了${talentStoneBaseData.length}条基础配置数据`);

        console.log('🎉 天赋石数据修复完成！');

    } catch (error) {
        console.error('❌ 修复天赋石数据失败:', error.message);
        throw error;
    } finally {
        if (connection) {
            await connection.end();
            console.log('🔌 数据库连接已关闭');
        }
    }
}

// 运行修复
async function main() {
    try {
        await fixTalentStoneData();
    } catch (error) {
        console.error('💥 修复脚本执行失败:', error.message);
        process.exit(1);
    }
}

// 如果直接运行此脚本
if (require.main === module) {
    main();
}

module.exports = fixTalentStoneData;
